package base;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Optional;
import java.util.stream.Stream;

/**
 * 枚举的使用
 */
@Getter
@AllArgsConstructor
public enum EnumDemo {

    SUCCESS(0, "成功"),
    ERROR(1, "失败");

    private final Integer code;
    private final String message;

    public static EnumDemo getByCode(Integer code) {
        return Optional.ofNullable(code)
                .flatMap(c -> Stream.of(EnumDemo.values())
                        .filter(x -> x.code.equals(c))
                        .findFirst())
                .orElse(null);
    }

    public static void main(String[] args) {
        System.out.println(EnumDemo.getByCode(null));
        System.out.println(EnumDemo.getByCode(0));
        System.out.println(EnumDemo.getByCode(1));
        System.out.println(EnumDemo.getByCode(2));
    }
}
